import React, {PureComponent} from "react";
import {connect} from "react-redux";
import {Button, Icon, notification, Popconfirm, Switch, Tooltip} from "antd";
import {
    changeInvoice,
    changeSettlement,
    getBusinessCodeList,
    getCurrencyList,
    getFeeList,
    getMyColumns,
    getRateList,
    query,
    queryExportLog,
    reCalculateFee,
    showColumns,
    showCreate,
    showExport,
    showExportLog,
    showPartExport,
    unSetOrder,
    updateDataSource
} from "./actions";
import {getPrincipal} from "../../lib/identity";
import {query as bankQuery} from "../bankInfo/actions";

class Toolbar extends PureComponent {
    state = {daiBan: false};
    handleShowCreate = () => {
        const {dispatch} = this.props;
        dispatch(showCreate());
        dispatch(getCurrencyList());
        dispatch(getFeeList());
        dispatch(getBusinessCodeList());
    };

    handleShowColumns = () => {
        const {dispatch} = this.props;
        dispatch(getMyColumns("ghOrderPayable"));
        dispatch(showColumns());
    };

    handleShowExport = () => {
        const {dispatch, selectedRowKeys} = this.props;
        dispatch(bankQuery({}));
        if (selectedRowKeys.length === 0) {
            dispatch(getRateList());
            dispatch(showExport());
        } else {
            dispatch(getRateList());
            dispatch(showPartExport(selectedRowKeys));
        }
    };

    handleExport = () => {
        const {dispatch, selectedRowKeys} = this.props;
        dispatch(getRateList());
        dispatch(showPartExport(selectedRowKeys));
    };

    handleShowExportLog = () => {
        const {dispatch} = this.props;
        dispatch(showExportLog());
        dispatch(queryExportLog());
    };

    reCalculateFee = () => {
        const {dispatch, selectedRowKeys, filter} = this.props;
        const baiBan = this.state.daiBan ? "daiBan" : "";
        dispatch(reCalculateFee(selectedRowKeys)).then(action => {
            if (action.error !== true) {
                dispatch(query({
                    ...filter, "userId": getPrincipal().id, daiBan: baiBan, type: "OUT"
                }));
            } else {
                notification.error({
                    message: '刷新费用失败'
                });
            }
        });
    };

    handleInvoice = () => {
        const {dispatch, selectedRowKeys, dataSource} = this.props;
        dispatch(changeInvoice(selectedRowKeys)).then(action => {
            selectedRowKeys.forEach(id => {
                const found = dataSource.find(item => item.id === id);
                if (found) {
                    found.outInvoice = "已开票";
                }
            });
            if (action.error === undefined || action.error === false) {
                notification.success({
                    message: '开票成功'
                });
            }
            dispatch(updateDataSource(dataSource));
        });
    };

    handleSettlement = () => {
        const {dispatch, selectedRowKeys, dataSource} = this.props;
        dispatch(changeSettlement(selectedRowKeys)).then(action => {
            selectedRowKeys.forEach(id => {
                const found = dataSource.find(item => item.id === id);
                if (found) {
                    found.outSettlement = "已结算";
                }
            });
            if (action.error === undefined || action.error === false) {
                notification.success({
                    message: '结算成功'
                });
            }
            dispatch(updateDataSource(dataSource));
        });
    };

    onChangeDaiBan = (value, e) => {
        const {dispatch} = this.props;
        console.info(value);
        this.setState({daiBan: value});
        const baiBan = value ? "daiBan" : "";
        dispatch(query({
            "userId": getPrincipal().id, daiBan: baiBan, type: "OUT"
        }));
    };

    unSet = () => {
        const {dispatch, selectedRowKeys, filter} = this.props;
        const baiBan = this.state.daiBan ? "daiBan" : "";
        dispatch(unSetOrder(selectedRowKeys)).then(action => {
            if (action.error !== true) {
                notification.success({
                    message: '取消成功'
                });
                dispatch(query({
                    ...filter, "userId": getPrincipal().id, daiBan: baiBan, type: "IN"
                }));
            } else {
                notification.error({
                    message: '刷新费用失败'
                });
            }
        });
    };

    render() {
        // const canShow = getPrincipal().admin;
        const canShow = true;
        return (
            <div className="actions">
                <Button onClick={this.handleShowColumns}><Icon type="ordered-list"/>自定义列</Button>
                <Popconfirm title="确定把勾选的记录改为已开票?" okText="是" cancelText="否"
                            onConfirm={() => this.handleInvoice()}>
                    <Button disabled={this.props.selectedRowKeys.length === 0}>已开票</Button>
                </Popconfirm>
                <Popconfirm title="确定把勾选的记录改为已结算?" okText="是" cancelText="否"
                            onConfirm={() => this.handleSettlement()}>
                    <Button disabled={this.props.selectedRowKeys.length === 0}>已结算</Button>
                </Popconfirm>
                {canShow && <span>
                    <Popconfirm title="确定取消封账?" okText="是" cancelText="否"
                                onConfirm={() => this.unSet()}>
                        <Button disabled={this.props.selectedRowKeys.length === 0}>取消封账</Button>
                    </Popconfirm>
                    <Button disabled={this.props.selectedRowKeys.length === 0}
                            onClick={() => this.reCalculateFee()}>重新计算费用</Button>
                <Popconfirm title="是否已经筛选了需要导出的数据?" okText="是" cancelText="否"
                            onConfirm={() => this.handleShowExport()}>
                    <Tooltip placement="bottom" title={"支持搜索后导出和勾选后导出"}><Button><Icon
                        type="export"/>导 出</Button></Tooltip>
                </Popconfirm>
                    {/*<Button disabled={this.props.selectedRowKeys.length === 0} onClick={this.handleExport}><Icon
                    type="export"/>勾选导出</Button>*/}
                    <Button onClick={this.handleShowExportLog}>查看导出记录</Button>
                </span>}
                <Switch checkedChildren="打开待办" unCheckedChildren="关闭待办"
                        checked={this.state.daiBan === true}
                        onChange={this.onChangeDaiBan}/>
            </div>

        );
    }
}

const mapStateToProps = state => {
    return {
        ...state.ghOrderPayable.list
    };
};

export default connect(mapStateToProps)(Toolbar);
